/**
 * 
 */
package cz.cvut.fit.crhonjar.mi.paa.dunedin;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;

import cz.cvut.fit.crhonjar.mi.paa.dunedin.exception.DunedinException;
import cz.cvut.fit.crhonjar.mi.paa.dunedin.manager.ProblemManager;
import cz.cvut.fit.crhonjar.mi.paa.dunedin.reader.ProblemReader;
import cz.cvut.fit.crhonjar.mi.paa.dunedin.solver.SolverFactory;

/**
 * @author Jarec
 * 
 */
public class Main {

	/**
	 * @param args
	 */
	public static void main(final String[] args) {
		System.out.println("\n>> Start\n");
		final BigDecimal start = new BigDecimal(System.nanoTime())
				.divide(new BigDecimal(1000000));
		if (args.length == 1) {

			try {

				final ProblemReader r = new ProblemReader(new FileReader(
						args[0]));
				// ProblemReader r = new ProblemReader(new
				// StringReader("10 2 4 3 0 0 2 0\n"));

				for (ProblemManager m : r.getManagers()) {
					// ProblemManager m = r.getManagerForId(31);
					m.accept(SolverFactory.getBruteForceBFSSolver());
					System.out
							.println("Problem ID <" + m.getId() + "> started");
					m.solve();
					System.out.println(m.solveToString());
					m.accept(SolverFactory.getBruteForceDFSSolver());
					m.solve();
					System.out.println(m.solveToString());
					m.accept(SolverFactory.getDistanceFromFinalSolver());
					m.solve();
					System.out.println(m.solveToString());
					m.accept(SolverFactory.getRankingSolver());
					m.solve();
					System.out.println(m.solveToString());
					m.accept(SolverFactory.getSquareOfDistanceFromFinalSolver());
					m.solve();
					System.out.println(m.solveToString());
					m = null;
					System.out.println();
				}

			} catch (final FileNotFoundException e) {
				System.err.println("File " + args[0] + " do NOT exist!");
				System.err.println(e.getMessage());
				// e.printStackTrace();
			} catch (final IOException e) {
				System.err.println("Cannot read selected file!");
				System.err.println(e.getMessage());
				// e.printStackTrace();
			} catch (final DunedinException e) {
				System.err.println(e.getMessage());
			} catch (final IllegalAccessException e) {
				System.err.println(e.getMessage());
			}

		} else {
			System.err.println("Please provide data file as parametr!");
		}
		final BigDecimal stop = new BigDecimal(System.nanoTime())
				.divide(new BigDecimal(1000000));
		System.out.println("\n<< Stop ("
				+ (stop.subtract(start).setScale(3, RoundingMode.HALF_UP))
				+ " ms)\n");

	}
}
